<?php


namespace app\controllers;

use app\exception\ErrorCode;
use app\exception\OaException;
use app\models\UploadForm;
use app\models\UserDto;
use app\service\ApplyService;
use Yii;

use app\models\request\ReqActListPage;
use app\models\request\ReqId;
use app\service\ActService;
use yii\web\UploadedFile;


class ActUploadController extends BaseJsonController
{

    /**
     * @param $req ReqId
     * @param $userInfo UserDto
     * @return \app\models\response\RespLogin|mixed
     * @throws \app\exception\OaException
     */
    public function doService($req,$userInfo)
    {

        $service  = new ApplyService();
        $entity = $service->findByIdAndUserId($req->getId(),$userInfo->getUserId());
        if ($entity->getStatus() == 4) {
            throw new OaException(ErrorCode::feedback_already);
        }
        if ($entity->getStatus() == 3) {
            throw new OaException(ErrorCode::ACT_FINISHED);
        }
        if ($entity->getStatus() == 5) {
            throw new OaException(ErrorCode::cancel_already);
        }
        if ($entity->getStatus() == 6) {
            throw new OaException(ErrorCode::cancel_ok);
        }




        $model = new UploadForm();
        $model->id = $entity->getId();
        if (Yii::$app->request->isPost) {
            $model->file = UploadedFile::getInstanceByName('file');
            if ($filePath = $model->upload()) {
                $entity->setFeedbackMaterial($filePath);
                $entity->setStatus(4);
                $entity->save();
                return [
                    'success' => true,
                    'filePath' => $filePath,
                ];
            }
        }

        throw new OaException(ErrorCode::upload_err);
    }

    protected function injectionReq()
    {
        return new ReqId();
    }

    public function needLogin(){
        return true;
    }
}